<template>
  <div class="settings">
    <el-drawer v-model="drawer" :direction="direction" size="300px" :before-close="handleClose">
      <template #header>
        <div class="slotDrawer">
          <div class="head">
            <span>主题设置</span>
          </div>
        </div>
      </template>
      <div class="drawerMain">
        <div class="title">
          <span class="line"></span>
          <span class="text">全局主题</span>
          <span class="line"></span>
        </div>
        <div class="settingsBg">
          <span>主题颜色</span>
          <el-color-picker
            @change="activeModel"
            :validate-event="true"
            v-model="color"
            :predefine="predefineColors"
            prop="nameRgb"
          />
        </div>
        <div class="darkMode">
          <span>暗黑模式</span>
          <el-switch
            v-model="value2"
            class="mt-2"
            style="margin-left: 24px"
            inline-prompt
            :active-icon="Sunny"
            :inactive-icon="Moon"
            size="large"
            @change="darkModel"
          />
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import mittBus from '@/utils/mittBus'
import { ref } from 'vue'
import { Sunny, Moon } from '@element-plus/icons-vue'
const drawer = ref(false)
const value2 = ref(true)
const dark = ref(false)
const color = ref('#409EFF')
// 预览颜色
const predefineColors = ref([
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577'
])
const direction = ref<'ltr' | 'rtl' | 'ttb' | 'btt'>('rtl')
// 监听事件
mittBus.on('openDrawer', (data: any) => {
  drawer.value = data
})
// 关闭弹窗
const handleClose = (done: () => void) => {
  done()
}
// 切换模式
const darkModel = () => {
  dark.value = !dark.value
  const html = document.documentElement
  dark.value ? (html.className = 'dark') : (html.className = '')
  mittBus.emit('switchMapColor', dark.value)
}
// 主题颜色改变时触发的事件
const activeModel = (value: string | null) => {
  if (value) {
    document.documentElement.style.setProperty('--el-color-primary', value)
    document.documentElement.style.setProperty('--el-color-danger', value)
  }
}
</script>

<style lang="less" scoped>
.settings {
  :deep(.el-drawer__header) {
    margin-bottom: 0;
  }
  .slotDrawer {
    .head {
      // border-bottom: 1px solid var(--el-border-color);
      span {
        color: #333;
        font-weight: 600;
      }
    }
  }
  .drawerMain {
    padding-top: 20px;
    border-top: 1px solid var(--el-border-color);
    .title {
      display: flex;
      align-items: center;
      font-size: 600;
      .line {
        flex: 1;
        height: 1px;
        background-color: var(--el-border-color);
      }
      .text {
        width: 100px;
        text-align: center;
        // flex: 1;
        // margin: 0 10px;
      }
    }
    .settingsBg,
    .darkMode {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    > div {
      margin-bottom: 30px;
    }
  }
}
</style>
